<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单号" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入工单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户型号" prop="customerModel">
        <el-select v-model="queryParams.customerModel" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.customer_model" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-select v-model="queryParams.color" filterable placeholder="请选择颜色" clearable>
          <el-option v-for="dict in dict.type.colour" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="包装日期" prop="packagingDate">
        <el-date-picker clearable v-model="queryParams.packagingDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择包装日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="销售订单号" prop="clientOrderNumber" label-width="85px">
        <el-input v-model="queryParams.clientOrderNumber" placeholder="请输入销售订单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['packageWeighing:package:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['packageWeighing:package:edit']">修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['packageWeighing:package:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="audit" plain size="mini" :disabled="multiple" @click="handleAudit"
          v-hasPermi="['packageWeighing:package:audit']">审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="audit" plain size="mini" :disabled="multiple" @click="UnhandleAudit"
          v-hasPermi="['packageWeighing:package:unaudit']">反审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="audit" plain size="mini" :disabled="multiple" v-hasPermi="['packageWeighing:package:print']"
          @click="dialogVisibleprint = true">打印</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['packageWeighing:package:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="packageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆ID" align="center" prop="bicycleId" />
      <el-table-column label="包装日期" align="center" prop="packagingDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.packagingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工单号" align="center" prop="orderId" />
      <el-table-column label="客户订单号" align="center" prop="clientOrderNumber" />
      <el-table-column label="产品编码" align="center" prop="productId" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="规格型号" align="center" prop="specifications">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.specification_and_model" :value="scope.row.specifications" />
        </template>
      </el-table-column>
      <el-table-column label="客户型号" align="center" prop="customerModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_model" :value="scope.row.customerModel" />
        </template>
      </el-table-column>
      <el-table-column label="外箱尺寸" align="center" prop="cartonSize">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.carton_size" :value="scope.row.cartonSize" />
        </template>
      </el-table-column>
      <el-table-column label="颜色" align="center" prop="color">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.colour" :value="scope.row.color" />
        </template>
      </el-table-column>
      <el-table-column label="仓库" align="center" prop="warehouse" />
      <el-table-column label="客户名称" align="center" prop="clientName" />
      <el-table-column label="箱数" align="center" prop="cartonNumbers" />
      <el-table-column label="毛重" align="center" prop="grossWeight" />
      <el-table-column label="净重" align="center" prop="net" />

      <el-table-column label="审核状态" align="center" prop="packagingFlag">
        <template #default="{ row }">
          <el-tag v-if="row.packagingFlag == 1" type="success">已审核</el-tag>
          <el-tag v-if="row.packagingFlag == 2" type="danger">未审核</el-tag>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['packageWeighing:package:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['packageWeighing:package:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改包装称重对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="车辆ID" prop="bicycleId">
          <el-input v-model="form.bicycleId" :disabled="disabled" placeholder="请输入车辆ID" @change="bicycleIdChange" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="包装日期" prop="packagingDate">
              <el-date-picker clearable v-model="form.packagingDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择包装日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单号" prop="orderId">
              <el-input v-model="form.orderId" disabled />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户订单号" prop="clientOrderNumber">
              <el-input v-model="form.clientOrderNumber" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品编码" prop="productId">
              <el-input v-model="form.productId" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="specifications">
              <el-input v-model="form.specifications" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户型号" prop="customerModel">
              <el-input v-model="form.customerModel" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="颜色" prop="colour">
              <el-input v-model="form.colour" disabled />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="外箱尺寸" prop="cartonSize">
              <el-input v-model="form.cartonSize" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="仓库" prop="warehouse">
              <el-input v-model="form.warehouse" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">

            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="箱数" prop="cartonNumbers">
              <el-select v-model="form.cartonNumbers">
                <el-option label="一箱一辆" value="1"></el-option>
                <el-option label="一箱俩辆" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毛重(KG)" prop="grossWeight">
              <el-input v-model.number="form.grossWeight" type="number" placeholder="请输入毛重" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="净重(KG)" prop="net">
              <el-input v-model.number="form.net" type="number" placeholder="请输入净重" />
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 打印车辆显示 -->
    <el-dialog title="提示" :visible.sync="dialogVisibleprint" width="30%" :before-close="handleClose">
      <el-button type="audit" plain size="mini" v-print="print" @click="printOut">打印（出口）</el-button>
      <el-button type="audit" plain size="mini" v-print="print" @click="printIn">打印（内销）</el-button>
      <span slot="footer" class="dialog-footer">
        <el-button @click="printClose">取 消</el-button>
        <el-button type="primary" @click="printClose">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 打印车辆ID页面 -->
    <div class="qrCode-container" v-if="printun">
      <div id="printQrCode">
        <div v-for="(item, index) in this.printQrCode" :key="index" class="qrCode-content">
          <vue-qr ref="qrCode" :text="text(item).toString()"> </vue-qr>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { listPackage, getPackage, delPackage, addPackage, updatePackage, selectStatus, updateAudit, updateUnAudit } from "@/api/storage/packageWeighing/package";
import print from 'vue-print-nb'
import VueQr from 'vue-qr'
export default {
  name: "Package",
  dicts: ['colour', 'customer_model', 'carton_size', 'specification_and_model'],
  components: { VueQr },
  directives: {
    print
  },
  computed: {
    text() {
      return function (val) {
        const a = {
          "客户订单号": val.clientOrderNumber, "产品品号": val.productId, "产品名称": val.productName, "车辆ID": val.bicycleId, "车架号": val.frameId
          , "电机号": val.motorId, "控制器号": val.controllerId, "电机号": val.motorId, "仪表号": val.meterId
        }
        const b = {
          "clientOrderNumber": val.clientOrderNumber, "productId": val.productId, "productName": val.productName, "bicycleId": val.bicycleId, "frameId": val.frameId
          , "motorId": val.motorId, "controllerId": val.controllerId, "motorId": val.motorId, "meterId": val.meterId
        }
        let c = ""
        if (this.export == true) {
          c = a
        } else {
          c = b
        }
        return JSON.stringify(c);
      }
    }
  },

  data() {
    return {
      // 遮罩层
      loading: true,
      //打印车辆内销、出库判断
      export: true,
      //判断打印车辆页面
      dialogVisibleprint: false,
      //判断打印界面
      printun: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 包装称重表格数据
      packageList: [],
      // 弹出层标题
      title: "",
      //禁止修改车辆ID
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
      },
      //打印二维码页面选择
      printQrCode: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bicycleId: [
          { required: true, message: "车辆ID不能为空", trigger: "blur" }
        ],
        grossWeight: [
          { required: true, message: "毛重不能为空", trigger: "blur" }
        ],
        net: [
          { required: true, message: "净重不能为空", trigger: "blur" }
        ],
      },
      print: {
        id: 'printQrCode',
        popTitle: '配置页眉标题', // 打印配置页上方的标题
        // 查看是否添加需求对打印进行优化
        // preview: false, // 是否启动预览模式，默认是false
        // previewTitle: '预览的标题', // 打印预览的标题
        // previewPrintBtnLabel: '预览结束，开始打印', // 打印预览的标题下方的按钮文本，点击可进入打印
        // zIndex: 20002, // 预览窗口的z-index，默认是20002，最好比默认值更高
        // openCallback() {
        //   this.daying=true
        // }, // 调用打印时的callback
        // closeCallback() { console.log('关闭了打印工具！') }, // 关闭打印的callback(无法区分确认or取消)
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    printClose() {
      this.dialogVisibleprint = false
      this.printun = false
    },
    printOut() {
      this.export = false
      this.printun = true
    },
    printIn() {
      this.export = true
      this.printun = true
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.printun = false
          done();
        })
        .catch(_ => { });
    },
    /** 查询包装称重列表 */
    getList() {
      this.loading = true;
      listPackage(this.queryParams).then(response => {
        this.packageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //监听车辆ＩＤ变化事件查询详情
    bicycleIdChange(e) {
      getPackage(e).then(response => {
        this.form = response.data;
        this.form.packagingDate = new Date()
        this.form.cartonNumbers = "1"
      });

    },
    // 表单重置
    reset() {
      this.form = {
        bicycleId: null,
        controllerId: null,
        frameId: null,
        motorId: null,
        batteryId: null,
        meterId: null,
        packagingDate: null,
        grossWeight: null,
        net: null,
        orderId: null,
        audit: null,
        controllerFlag: null,
        cartonNumbers: null,
        orderFlag: null,
        packagingFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.printQrCode = selection
      this.ids = selection.map(item => item.bicycleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.disabled = false
      this.title = "添加包装称重";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.disabled = true
      const bicycleId = row.bicycleId || this.ids
      getPackage(bicycleId).then(response => {
        if (response.data.packagingFlag == 1) {
          this.$message.error("已审核无法修改")
        } else {
          getPackage(bicycleId).then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "修改包装称重";
          });
        }
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.packagingFlag != 2) {
            addPackage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            updatePackage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审核操作 */
    handleAudit(row) {
      const bicycleIds = row.ticketsId || this.ids;
      updateAudit(bicycleIds);
      this.getList();
      this.$modal.msgSuccess("审核成功");

    },
    UnhandleAudit(row) {
      const bicycleIds = row.ticketsId || this.ids;
      updateUnAudit(bicycleIds);
      this.getList();
      this.$modal.msgSuccess("反审核成功");

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bicycleIds = row.bicycleId || this.ids;
      this.$modal.confirm('是否确认删除包装称重编号为"' + bicycleIds + '"的数据项？').then(function () {
        return delPackage(bicycleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('packageWeighing/package/export', {
        ...this.queryParams
      }, `package_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
